package com.automannn.atm_back.codegenerate.generate;

import com.automannn.atm_back.codegenerate.pojo.CreateFileProperty;
import com.automannn.atm_back.codegenerate.pojo.oneToMany.SubTableEntity;
import freemarker.template.TemplateException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.*;

/**
 * @author chenkh
 * @time 2020/6/27 17:17
 */
public class CgFormCodeGenerate implements GenerateContext{
    private static final Log a = LogFactory.getLog(CgFormCodeGenerate.class);
    private String entityPackage = "test";
    private String entityName = "Person";
    private String tableName = "person";
    private String ftlDescription = "公告";
    private String primaryKeyPolicy = "uuid";
    private String sequenceCode = "";
    private String[] foreignKeys;
    public static int n = 1;
    private String o;
    private SubTableEntity u;
    private GenerateEntity v;
    private CreateFileProperty w;
    private String x;
    private String[] y;
    private GenerateEntity z;
    private static CreateFileProperty B = new CreateFileProperty();

    public CgformCodeGenerate() {
    }

    public CgformCodeGenerate(CreateFileProperty createFileProperty2, GenerateEntity generateEntity) {
        this.entityName = generateEntity.getEntityName();
        this.entityPackage = generateEntity.getEntityPackage();
        this.tableName = generateEntity.getTableName();
        this.ftlDescription = generateEntity.getFtlDescription();
        n = 1;
        B = createFileProperty2;
        B.setJspMode(createFileProperty2.getJspMode());
        this.primaryKeyPolicy = generateEntity.getPrimaryKeyPolicy();
        this.sequenceCode = "";
        this.z = generateEntity;
    }

    public CgformCodeGenerate(SubTableEntity sub, GenerateEntity subG, CreateFileProperty subFileProperty, String policy, String[] array) {
        this.entityName = subG.getEntityName();
        this.entityPackage = subG.getEntityPackage();
        this.tableName = subG.getTableName();
        this.ftlDescription = subG.getFtlDescription();
        B = subFileProperty;
        n = 1;
        this.primaryKeyPolicy = policy;
        this.sequenceCode = "";
        this.z = subG;
        this.foreignKeys = array;
        this.u = sub;
        this.v = subG;
        this.w = subFileProperty;
        this.x = policy;
    }

    public Map<String, Object> e() {
        Map<String, Object> data = new HashMap();
        Map<String, String> fieldMeta = new HashMap();
        data.put("bussiPackage", CodeResourceUtil.bussiPackage);
        data.put("entityPackage", this.entityPackage);
        data.put("entityName", this.entityName);
        data.put("tableName", this.tableName);
        data.put("ftl_description", this.ftlDescription);
        data.put(org.jeecgframework.codegenerate.util.def.a.an, CodeResourceUtil.JEECG_GENERATE_TABLE_ID);
        data.put(org.jeecgframework.codegenerate.util.def.a.ap, this.primaryKeyPolicy);
        data.put(org.jeecgframework.codegenerate.util.def.a.aq, this.sequenceCode);
        data.put("ftl_create_time", org.jeecgframework.codegenerate.util.a.a(new Date()));
        data.put("foreignKeys", this.foreignKeys);
        data.put(org.jeecgframework.codegenerate.util.def.a.al, StringUtils.isNotEmpty(CodeResourceUtil.JEECG_UI_FIELD_REQUIRED_NUM) ? Integer.parseInt(CodeResourceUtil.JEECG_UI_FIELD_REQUIRED_NUM) : -1);
        data.put(org.jeecgframework.codegenerate.util.def.a.ao, StringUtils.isNotEmpty(CodeResourceUtil.JEECG_UI_FIELD_SEARCH_NUM) ? Integer.parseInt(CodeResourceUtil.JEECG_UI_FIELD_SEARCH_NUM) : -1);
        data.put(org.jeecgframework.codegenerate.util.def.a.am, n);

        try {
            List<CgFormFieldEntity> columns = this.z.deepCopy().getCgFormHead().getColumns();
            Iterator i$ = columns.iterator();

            while(i$.hasNext()) {
                CgFormFieldEntity cf = (CgFormFieldEntity)i$.next();
                String type = cf.getType();
                if ("string".equalsIgnoreCase(type)) {
                    cf.setType("java.lang.String");
                } else if ("Date".equalsIgnoreCase(type)) {
                    cf.setType("java.util.Date");
                } else if ("double".equalsIgnoreCase(type)) {
                    cf.setType("java.lang.Double");
                } else if ("int".equalsIgnoreCase(type)) {
                    cf.setType("java.lang.Integer");
                } else if ("BigDecimal".equalsIgnoreCase(type)) {
                    cf.setType("java.math.BigDecimal");
                } else if ("Text".equalsIgnoreCase(type)) {
                    cf.setType("java.lang.String");
                } else if ("Blob".equalsIgnoreCase(type)) {
                    cf.setType("java.sql.Blob");
                }

                String fieldName = cf.getFieldName();
                String fieldNameV = JeecgReadTable.formatField(fieldName);
                cf.setFieldName(fieldNameV);
                fieldMeta.put(fieldNameV, fieldName.toUpperCase());
            }

            List<CgFormFieldEntity> pageColumns = new ArrayList();
            List<CgFormFieldEntity> pageAreatextColumns = new ArrayList();
            Iterator i$ = columns.iterator();

            label87:
            while(true) {
                while(true) {
                    CgFormFieldEntity cf;
                    do {
                        do {
                            if (!i$.hasNext()) {
                                data.put("cgformConfig", this.z);
                                data.put("fieldMeta", fieldMeta);
                                data.put("columns", columns);
                                data.put("pageColumns", pageColumns);
                                data.put("pageAreatextColumns", pageAreatextColumns);
                                data.put("buttons", this.z.getButtons() == null ? new ArrayList(0) : this.z.getButtons());
                                data.put("buttonSqlMap", this.z.getButtonSqlMap() == null ? new HashMap(0) : this.z.getButtonSqlMap());
                                data.put("buttonJavaMap", this.z.getButtonJavaMap() == null ? new HashMap(0) : this.z.getButtonJavaMap());
                                data.put("packageStyle", this.z.getPackageStyle());
                                break label87;
                            }

                            cf = (CgFormFieldEntity)i$.next();
                        } while(!StringUtils.isNotEmpty(cf.getIsShow()));
                    } while(!"Y".equalsIgnoreCase(cf.getIsShow()));

                    if (!"textarea".equals(cf.getShowType()) && !"umeditor".equals(cf.getShowType())) {
                        pageColumns.add(cf);
                    } else {
                        pageAreatextColumns.add(cf);
                    }
                }
            }
        } catch (Exception var9) {
            var9.printStackTrace();
        }

        long serialVersionUID = org.jeecgframework.codegenerate.util.c.D() + org.jeecgframework.codegenerate.util.c.H();
        data.put("serialVersionUID", String.valueOf(serialVersionUID));
        return data;
    }

    public void generateToFile() throws TemplateException, IOException {
        a.info("----jeecg---Code----Generation----[单表模型:" + this.tableName + "]------- 生成中。。。");
        org.jeecgframework.codegenerate.generate.a codeFactory = new org.jeecgframework.codegenerate.generate.a();
        codeFactory.setProjectPath(this.z.getProjectPath());
        codeFactory.setPackageStyle(this.z.getPackageStyle());
        if (this.z.getCgFormHead().getJformType() == 1) {
            codeFactory.a(new CgformCodeGenerate(B, this.z));
        } else {
            codeFactory.a(new CgformCodeGenerate(this.u, this.v, this.w, "uuid", this.foreignKeys));
        }

        if (B.isJspFlag()) {
            if ("03".equals(B.getJspMode())) {
                codeFactory.b("onetomany/cgform_jspSubTemplate.ftl", "jspList");
            } else if ("06".equals(B.getJspMode())) {
                codeFactory.b("onetomany/cgform_jspBootstrapSubTemplate.ftl", "jspList");
            } else {
                if (StringUtils.isNotEmpty(this.o)) {
                    codeFactory.d(this.o, "jsp_add");
                    codeFactory.d(this.o.replace("doAdd", "doUpdate"), "jsp_update");
                } else if ("01".equals(B.getJspMode())) {
                    codeFactory.b("cgform_jspTableTemplate_add.ftl", "jsp_add");
                    codeFactory.b("cgform_jspTableTemplate_update.ftl", "jsp_update");
                } else if ("02".equals(B.getJspMode())) {
                    codeFactory.b("cgform_jspDivTemplate_add.ftl", "jsp_add");
                    codeFactory.b("cgform_jspDivTemplate_update.ftl", "jsp_update");
                } else if ("05".equals(B.getJspMode())) {
                    codeFactory.b("cgform_jspBootstrapTemplate_add.ftl", "jsp_add");
                    codeFactory.b("cgform_jspBootstrapTemplate_update.ftl", "jsp_update");
                }

                codeFactory.b("cgform_jspListTemplate.ftl", "jspList");
                codeFactory.b("cgform_jsListEnhanceTemplate.ftl", "jsList");
                codeFactory.b("cgform_jsEnhanceTemplate.ftl", "js");
            }
        }

        if (B.isServiceImplFlag()) {
            codeFactory.b("cgform_serviceImplTemplate.ftl", "serviceImpl");
        }

        if (B.isServiceIFlag()) {
            codeFactory.b("cgform_serviceITemplate.ftl", "service");
        }

        if (B.isActionFlag()) {
            codeFactory.b("cgform_controllerTemplate.ftl", "controller");
        }

        if (B.isEntityFlag()) {
            codeFactory.b("cgform_entityTemplate.ftl", "entity");
        }

        a.info("----jeecg----Code----Generation-----[单表模型：" + this.tableName + "]------ 生成完成。。。");
    }

    public void generateToFileUserDefined() throws TemplateException, IOException {
        a.info("----jeecg---Code----Generation----[单表模型:" + this.tableName + "]------- 生成中。。。");
        org.jeecgframework.codegenerate.generate.a codeFactory = new org.jeecgframework.codegenerate.generate.a();
        codeFactory.setProjectPath(this.z.getProjectPath());
        codeFactory.setPackageStyle(this.z.getPackageStyle());
        if (this.z.getCgFormHead().getJformType() == 1) {
            codeFactory.a(new CgformCodeGenerate(B, this.z));
        } else {
            codeFactory.a(new CgformCodeGenerate(this.u, this.v, this.w, "uuid", this.foreignKeys));
        }

        String path = B.getJspMode().replace(".", "/");
        a.info("----jeecg------path----[" + path + "]-------");
        if (B.isJspFlag()) {
            if (path.endsWith("onetomany")) {
                codeFactory.c(path + "/cgform_jspSubTemplate.ftl", "jspList");
            } else {
                if (StringUtils.isNotEmpty(this.o)) {
                    codeFactory.d(this.o, "jsp_add");
                    codeFactory.d(this.o.replace("doAdd", "doUpdate"), "jsp_update");
                } else {
                    codeFactory.c(path + "/cgform_jspTemplate_add.ftl", "jsp_add");
                    codeFactory.c(path + "/cgform_jspTemplate_update.ftl", "jsp_update");
                }

                codeFactory.c(path + "/cgform_jspListTemplate.ftl", "jspList");
                codeFactory.c(path + "/cgform_jsListEnhanceTemplate.ftl", "jsList");
                codeFactory.c(path + "/cgform_jsEnhanceTemplate.ftl", "js");
            }
        }

        if (B.isServiceImplFlag()) {
            codeFactory.c(path + "/cgform_serviceImplTemplate.ftl", "serviceImpl");
        }

        if (B.isServiceIFlag()) {
            codeFactory.c(path + "/cgform_serviceITemplate.ftl", "service");
        }

        if (B.isActionFlag()) {
            codeFactory.c(path + "/cgform_controllerTemplate.ftl", "controller");
        }

        if (B.isEntityFlag()) {
            codeFactory.c(path + "/cgform_entityTemplate.ftl", "entity");
        }

        a.info("----jeecg----Code----Generation-----[单表模型：" + this.tableName + "]------ 生成完成。。。");
    }

    public GenerateEntity g() {
        return this.z;
    }

    public void a(GenerateEntity cgformConfig) {
        this.z = cgformConfig;
    }

    public String f() {
        return this.o;
    }

    public void setCgformJspHtml(String cgformJspHtml) {
        this.o = cgformJspHtml;
    }

    static {
        B.setActionFlag(true);
        B.setServiceIFlag(true);
        B.setJspFlag(true);
        B.setServiceImplFlag(true);
        B.setJspMode("01");
        B.setPageFlag(true);
        B.setEntityFlag(true);
    }

}
